﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using Caliburn.Micro;
using Torrenter.Client.Main.Framework;
using Torrenter.Client.Main.Framework.Messages;
using Torrenter.Manager.Services;

namespace Torrenter.Client.Main.ViewModels
{
    [Export(typeof(ShellViewModel))]
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IHandle<OpenTabEvent>
    {

        private BindableCollection<ScreenType> _screenTypes;
        public BindableCollection<ScreenType> ScreensTypes
        {
            get
            {
                return _screenTypes ?? (_screenTypes = new BindableCollection<ScreenType>()
                                                           {
                                                               new ScreenType("Shows", typeof (ShowsViewModel)),
                                                               new ScreenType("Search", typeof (SearchViewModel)),
                                                               new ScreenType("Admin", typeof (AdminViewModel))
                                                           });
            }
        }

        protected ShowsViewModel ShowsViewModel;
        protected SearchViewModel SearchViewModel;

        public ShellViewModel()
        {
            IoC.Get<IEventAggregator>().Subscribe(this);
            IoC.BuildUp(this);

            DisplayName = "Torrenter";
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            ShowsViewModel = new ShowsViewModel();
            SearchViewModel = new SearchViewModel();
            ActivateItem(ShowsViewModel);
            ActivateItem(SearchViewModel);
        }

        public void CloseItem(Screen screen)
        {
            DeactivateItem(screen, true);
        }

        public void Handle(OpenTabEvent message)
        {
            ActivateItem(message.Screen);
        }

        public void OpenTab(ScreenType screenType)
        {
            ActivateItem((IScreen)screenType.Type.Assembly.CreateInstance(screenType.Type.FullName));
            //ActivateItem(screenType.);
        }

    }
}